home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / pt-const.h < prev    next >
C/C++ Source or Header  |  1996-11-19  |  11KB  |  369 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_tree_const_h)
  24. #define octave_tree_const_h 1
  25.  
  26. #if defined (__GNUG__)
  27. #pragma interface
  28. #endif
  29.  
  30. #include <string>
  31.  
  32. class ostream;
  33.  
  34. #include "Range.h"
  35. #include "mx-base.h"
  36. #include "oct-alloc.h"
  37. #include "str-vec.h"
  38.  
  39. #include "pt-fvc.h"
  40.  
  41. class Octave_map;
  42. class octave_value_list;
  43.  
  44. class tree_walker;
  45.  
  46. #include "ov.h"
  47.  
  48. class
  49. tree_constant : public tree_fvc
  50. {
  51. public:
  52.  
  53.   enum magic_colon { magic_colon_t };
  54.   enum all_va_args { all_va_args_t };
  55.  
  56.   // Constructors.  It is possible to create the following types of
  57.   // constants:
  58.   //
  59.   // constant type    constructor arguments
  60.   // -------------    ---------------------
  61.   // unknown          none
  62.   // real scalar      double
  63.   // real matrix      Matrix
  64.   //                  DiagMatrix
  65.   //                  RowVector
  66.   //                  ColumnVector
  67.   // complex scalar   Complex
  68.   // complex matrix   ComplexMatrix
  69.   //                  ComplexDiagMatrix
  70.   //                  ComplexRowVector
  71.   //                  ComplexColumnVector
  72.   // char matrix      charMatrix
  73.   // string           char* (null terminated)
  74.   //                  string
  75.   //                  charMatrix
  76.   // range            double, double, double
  77.   //                  Range
  78.   // map              Octave_map
  79.   // magic colon      tree_constant::magic_colon
  80.   // all_va_args      tree_constant::all_va_args
  81.  
  82.   tree_constant (void)
  83.     : tree_fvc (), val (), orig_text () { }
  84.  
  85.   tree_constant (double d, int l = -1, int c = -1)
  86.     : tree_fvc (l, c), val (d), orig_text () { }
  87.  
  88.   tree_constant (const Matrix& m)
  89.     : tree_fvc (), val (m), orig_text () { }
  90.  
  91.   tree_constant (const DiagMatrix& d)
  92.     : tree_fvc (), val (d), orig_text () { }
  93.  
  94.   tree_constant (const RowVector& v, int pcv = -1)
  95.     : tree_fvc (), val (v, pcv), orig_text () { }
  96.  
  97.   tree_constant (const ColumnVector& v, int pcv = -1)
  98.     : tree_fvc (), val (v, pcv), orig_text () { }
  99.  
  100.   tree_constant (const Complex& C, int l = -1, int c = -1)
  101.     : tree_fvc (l, c), val (C), orig_text () { }
  102.  
  103.   tree_constant (const ComplexMatrix& m)
  104.     : tree_fvc (), val (m), orig_text () { }
  105.  
  106.   tree_constant (const ComplexDiagMatrix& d)
  107.     : tree_fvc (), val (d), orig_text () { }
  108.  
  109.   tree_constant (const ComplexRowVector& v, int pcv = -1)
  110.     : tree_fvc (), val (v, pcv), orig_text () { }
  111.  
  112.   tree_constant (const ComplexColumnVector& v, int pcv = -1)
  113.     : tree_fvc (), val (v, pcv), orig_text () { }
  114.  
  115.   tree_constant (const char *s, int l = -1, int c = -1)
  116.     : tree_fvc (l, c), val (s), orig_text () { }
  117.  
  118.   tree_constant (const string& s, int l = -1, int c = -1)
  119.     : tree_fvc (l, c), val (s), orig_text () { }
  120.  
  121.   tree_constant (const string_vector& s, int l = -1, int c = -1)
  122.     : tree_fvc (l, c), val (s), orig_text () { }
  123.  
  124.   tree_constant (const charMatrix& chm, bool is_string = false)
  125.     : tree_fvc (), val (chm, is_string), orig_text () { }
  126.  
  127.   tree_constant (double base, double limit, double inc)
  128.     : tree_fvc (), val (base, limit, inc), orig_text () { }
  129.  
  130.   tree_constant (const Range& r)
  131.     : tree_fvc (), val (r), orig_text () { }
  132.  
  133.   tree_constant (const Octave_map& m)
  134.     : tree_fvc (), val (m), orig_text () { }
  135.  
  136.   tree_constant (tree_constant::magic_colon, int l = -1, int c = -1)
  137.     : tree_fvc (l, c), val (octave_value::magic_colon_t), orig_text () { }
  138.  
  139.   tree_constant (tree_constant::all_va_args, int l = -1, int c = -1)
  140.     : tree_fvc (l, c), val (octave_value::all_va_args_t), orig_text () { }
  141.  
  142.   tree_constant (const octave_value& v, int l = -1, int c = -1)
  143.     : tree_fvc (l, c), val (v), orig_text () { }
  144.  
  145.   tree_constant (const tree_constant& a)
  146.     : tree_fvc (), val (a.val), orig_text () { }
  147.  
  148.   ~tree_constant (void) { }
  149.  
  150.   tree_constant& operator = (const tree_constant& a)
  151.     {
  152.       if (this != &a)
  153.     {
  154.       tree_fvc::operator = (a);
  155.       val = a.val;
  156.     }
  157.       return *this;
  158.     }
  159.  
  160.   void *operator new (size_t size)
  161.     { return allocator.alloc (size); }
  162.  
  163.   void operator delete (void *p, size_t size)
  164.     { allocator.free (p, size); }
  165.  
  166.   // Indexed assignment.
  167.  
  168.   octave_value index (const octave_value_list& idx) const
  169.     { return val.index (idx); }
  170.  
  171.   octave_value& reference (void)
  172.     {
  173.       val.make_unique ();
  174.       return val;
  175.     }
  176.  
  177.   octave_value value (void) const
  178.     {
  179.       return val;
  180.     }
  181.  
  182.   octave_value assign (const octave_value_list& idx, const octave_value& rhs)
  183.     {
  184.       val.assign (idx, rhs);
  185.       return val;
  186.     }
  187.  
  188.   // Type.  It would be nice to eliminate the need for this.
  189.  
  190.   bool is_constant (void) const { return true; }
  191.  
  192.   // Size.
  193.  
  194.   int rows (void) const { return val.rows (); }
  195.   int columns (void) const { return val.columns (); }
  196.  
  197.   // Does this constant have a type?  Both of these are provided since
  198.   // it is sometimes more natural to write is_undefined() instead of
  199.   // ! is_defined().
  200.  
  201.   bool is_defined (void) const { return val.is_defined (); }
  202.   bool is_undefined (void) const { return val.is_undefined (); }
  203.  
  204.   // Is this constant a particular type, or does it belong to a
  205.   // particular class of types?
  206.  
  207.   bool is_real_scalar (void) const { return val.is_real_scalar (); }
  208.   bool is_real_matrix (void) const { return val.is_real_matrix (); }
  209.   bool is_complex_scalar (void) const { return val.is_complex_scalar (); }
  210.   bool is_complex_matrix (void) const { return val.is_complex_matrix (); }
  211.   bool is_char_matrix (void) const { return val.is_char_matrix (); }
  212.   bool is_string (void) const { return val.is_string (); }
  213.   bool is_range (void) const { return val.is_range (); }
  214.   bool is_map (void) const { return val.is_map (); }
  215.   bool is_magic_colon (void) const { return val.is_magic_colon (); }
  216.   bool is_all_va_args (void) const { return val.is_all_va_args (); }
  217.  
  218.   // Are any or all of the elements in this constant nonzero?
  219.  
  220.   octave_value all (void) const { return val.all (); }
  221.   octave_value any (void) const { return val.any (); }
  222.  
  223.   // Other type stuff.
  224.  
  225.   bool is_real_type (void) const { return val.is_real_type (); }
  226.  
  227.   bool is_complex_type (void) const { return val.is_complex_type (); }
  228.  
  229.   bool is_scalar_type (void) const { return val.is_scalar_type (); }
  230.   bool is_matrix_type (void) const { return val.is_matrix_type (); }
  231.  
  232.   bool is_numeric_type (void) const
  233.     { return val.is_numeric_type (); }
  234.  
  235.   bool valid_as_scalar_index (void) const
  236.     { return val.valid_as_scalar_index (); }
  237.  
  238.   bool valid_as_zero_index (void) const
  239.     { return val.valid_as_zero_index (); }
  240.  
  241.   // Does this constant correspond to a truth value?
  242.  
  243.   bool is_true (void) const { return val.is_true (); }
  244.  
  245.   // Is at least one of the dimensions of this constant zero?
  246.  
  247.   bool is_empty (void) const
  248.     { return val.is_empty (); }
  249.  
  250.   // Are the dimensions of this constant zero by zero?
  251.  
  252.   bool is_zero_by_zero (void) const
  253.     { return val.is_zero_by_zero (); }
  254.  
  255.   // Values.
  256.  
  257.   double double_value (bool frc_str_conv = false) const
  258.     { return val.double_value (frc_str_conv); }
  259.  
  260.   Matrix matrix_value (bool frc_str_conv = false) const
  261.     { return val.matrix_value (frc_str_conv); }
  262.  
  263.   Complex complex_value (bool frc_str_conv = false) const
  264.     { return val.complex_value (frc_str_conv); }
  265.  
  266.   ComplexMatrix complex_matrix_value (bool frc_str_conv = false) const
  267.     { return val.complex_matrix_value (frc_str_conv); }
  268.  
  269.   charMatrix char_matrix_value (bool frc_str_conv = false) const
  270.     { return val.char_matrix_value (frc_str_conv); }
  271.  
  272.   charMatrix all_strings (void) const
  273.     { return val.all_strings (); }
  274.  
  275.   string string_value (void) const
  276.     { return val.string_value (); }
  277.  
  278.   Range range_value (void) const
  279.     { return val.range_value (); }
  280.  
  281.   Octave_map map_value (void) const;
  282.  
  283.   octave_value lookup_map_element (const string& ref,
  284.                    bool insert = false,
  285.                    bool silent = false);
  286.  
  287.   octave_value lookup_map_element (SLList<string>& list,
  288.                    bool insert = false,
  289.                    bool silent = false);
  290.  
  291.   ColumnVector vector_value (bool /* frc_str_conv */ = false,
  292.                  bool /* frc_vec_conv */ = false) const 
  293.     { return val.vector_value (); }
  294.  
  295.   ComplexColumnVector
  296.   complex_vector_value (bool /* frc_str_conv */ = false,
  297.             bool /* frc_vec_conv */ = false) const
  298.     { return val.complex_vector_value (); }
  299.  
  300.   // Binary and unary operations.
  301.  
  302.   friend octave_value do_binary_op (octave_value& a, octave_value& b,
  303.                     tree_expression::type t);
  304.  
  305.   friend octave_value do_unary_op (octave_value& a,
  306.                    tree_expression::type t);
  307.  
  308.   // Conversions.  These should probably be private.  If a user of this
  309.   // class wants a certain kind of constant, he should simply ask for
  310.   // it, and we should convert it if possible.
  311.  
  312.   octave_value convert_to_str (void) const
  313.     { return val.convert_to_str (); }
  314.  
  315.   void convert_to_row_or_column_vector (void)
  316.     { val.convert_to_row_or_column_vector (); }
  317.  
  318.   void maybe_mutate (void)
  319.     { val.maybe_mutate (); }
  320.  
  321.   // Increment or decrement this constant.
  322.  
  323.   void increment (void) { val.increment (); }
  324.  
  325.   void decrement (void) { val.decrement (); }
  326.  
  327.   void print (void);
  328.   void print (ostream& os, bool pr_as_read_syntax = false,
  329.           bool pr_orig_txt = true);
  330.  
  331.   void print_with_name (const string& name, bool print_padding = true);
  332.   void print_with_name (ostream& os, const string& name,
  333.             bool print_padding = true);
  334.  
  335.   octave_value eval (bool print_result);
  336.  
  337.   octave_value_list eval (bool, int, const octave_value_list&);
  338.  
  339.   // Store the original text corresponding to this constant for later
  340.   // pretty printing.
  341.  
  342.   void stash_original_text (const string& s);
  343.  
  344.   string original_text (void) const;
  345.  
  346.   void accept (tree_walker& tw);
  347.  
  348.   string type_name (void) const { return val.type_name (); }
  349.  
  350. private:
  351.  
  352.   // For custom memory management.
  353.   static octave_allocator allocator;
  354.  
  355.   // The actual value that this constant refers to.
  356.   octave_value val;
  357.  
  358.   // The original text form of this constant.
  359.   string orig_text;
  360. };
  361.  
  362. #endif
  363.  
  364. /*
  365. ;;; Local Variables: ***
  366. ;;; mode: C++ ***
  367. ;;; End: ***
  368. */
  369.